package com.carpedil.mq.service;

import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/** @author Administrator */
@Service
public class MqAckServiceImpl implements MqAckService {

  @Autowired RabbitTemplate rabbitTemplate;
  RabbitTemplate.ConfirmCallback callback =
      (CorrelationData correlationData, boolean b, String s) -> {
        // 获得MQ返回的唯一标志orderNum
        String id = correlationData.getId();
        System.out.println(id);
        // 返回投递结果
        System.out.println(b);
        // 系统日志信息(可已忽略)
        System.out.println(s);
      };

  @Override
  public void send(String message) {
    // 开启手动确认
    rabbitTemplate.setMandatory(true);
    // 绑定回调函数
    rabbitTemplate.setConfirmCallback(callback);
    // 组装关联数据
    CorrelationData correlationData = new CorrelationData("1");
    rabbitTemplate.convertAndSend("directExchange", "j178.one", message, correlationData);
  }
}
